using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using LocalizationTest.Tools;

namespace LocalizationTest
{
  public class WorldState2D : WorldState
  {
    private Vector2 vector;

    public Vector2 Vector
    {
      get { return vector; }
      set { vector = value; }
    }


    public WorldState2D() 
    {
      vector = new Vector2();
    }

    public WorldState2D(double _positionX, double _positionY)
    {
      vector = new Vector2(_positionX, _positionY);
    }

    public override object Clone()
    {
      return this.MemberwiseClone();
    }

    public override void Move(WorldState relativeState)
    {
      vector += ((WorldState2D)relativeState).vector;
    }

    public override void Kidnap()
    {
      vector = new Vector2(Globals.stateSpaceInitialX, Globals.stateSpaceInitialY);
    }

    public override void DrawShaded(double shade, Color color, string drawingID)
    {
      Color color2 = DrawingTools.GetShadedColor(shade, ref color);
      DebugDrawingTool.CreateCircle(drawingID, vector, Globals.rasterWidth / 5, Globals.rasterWidth / 5, color2);
    }

    public override void Draw(double probability, string drawingID)
    {
      int value = (int)((1.0 - probability * 10.0) * 255.0);
      //      int value = (int)((probability * 50) * 255.0);
      if (value < 0) value = 0;
      if (value > 255) value = 255;
      Color color = Color.FromArgb(value, value, value);
      DebugDrawingTool.CreateLine(drawingID,
        new Vector2(vector.x - Globals.rasterWidth / 2.0, vector.y),
        new Vector2(vector.x + Globals.rasterWidth / 2.0, vector.y),
        Globals.rasterWidth, color);
    }

    public override void DrawWithIntensity(int intensity, string drawingID, double rasterWidth)
    {
      if (intensity < 0) intensity = 0;
      if (intensity > 255) intensity = 255;
      Color color = Color.FromArgb(intensity, intensity, intensity);
      DebugDrawingTool.CreateLine(drawingID,
        new Vector2(vector.x - rasterWidth / 2.0, vector.y),
        new Vector2(vector.x + rasterWidth / 2.0, vector.y),
        rasterWidth, color);
    }

    public override void DrawHighlighted(string drawingID, Color color)
    {
      DebugDrawingTool.CreateFilledCircle(drawingID, vector, 10, color);
    }

    public override void DrawConnectingLine(WorldState otherWorldstate, string drawingID, Color color)
    {
      DebugDrawingTool.CreateLine(drawingID, 
        vector, (otherWorldstate as WorldState2D).Vector, 3, color );
    }

    public override List<FunctionWorldState> CreateListOfWorldStates(WorldState minimum, WorldState maximum, WorldState step)
    {
      List<FunctionWorldState> function = new List<FunctionWorldState>();
      Vector2 bottomLeftCorner = new Vector2((minimum as WorldState2D).Vector.x, (minimum as WorldState2D).Vector.y);
      Vector2 topRightCorner = new Vector2((maximum as WorldState2D).Vector.x, (maximum as WorldState2D).Vector.y);
      Vector2 stepLength = new Vector2((step as WorldState2D).Vector.x, (step as WorldState2D).Vector.y);
      for (double x = bottomLeftCorner.x; x <= topRightCorner.x; x += stepLength.x)
      {
        for (double y = bottomLeftCorner.y; y <= topRightCorner.y; y += stepLength.y)
        {
          function.Add(new FunctionWorldState(new WorldState2D(x, y), 10));
        }
      }
      return function;
    }

    public override string ToString()
    {
      return this.GetType().Name + "|" + vector.x.ToString() + ";" + vector.y.ToString();
    }

    public override WorldState Parse(string str)
    {
      string[] parts = str.Split(';');
      return new WorldState2D(double.Parse(parts[0]), double.Parse(parts[1]));
    }

    public override double[] ToDoubleArray()
    {
      double[] toReturn = new double[2];
      toReturn[0] = vector.x;
      toReturn[1] = vector.y;
      return toReturn;
    }

    public override void FromDoubleArray(double[] doubleArray)
    {
      vector.x = doubleArray[0];
      vector.y = doubleArray[1];
    }
  }
}
